<extend name="common:commonNewerTemplate" />
<block name="main_content">
<div id="content">
	<style>
		.addressSelect{
			color: #000;
		    float: right;
		    font-size: 20px;
		    font-weight: bold;
		    line-height: 20px;
		    opacity: 0.2;
		    text-shadow: 0 1px 0 #fff;
		}
		.alert .addressSelect{
			line-height: 20px;
		    position: relative;
		    right: -21px;
		    top: -2px;
		}
	</style>
	<div id="content-header">
		<div id="breadcrumb">
			<a class="tip-bottom" href="#"
				data-original-title="Go to Home"><i class="icon-home"></i> 首页</a>
			<a href="#">经销商</a> <a class="current" href="#">经销商订单录入</a>
		</div>
	</div>
	<div class="container-fluid" id="biz_dealer" dealer_id='' dealer_name='' dealer_user_id=''>
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-title">
						<span class="icon"> <i class="icon-info-sign"></i>
						</span>
						<h5>经销商信息</h5>
					</div>
					<div class="widget-content nopadding" >

							<div class="form-actions" style="margin-top:0px;margin-bottom:0px;">
								<input type="button" value="加载经销商信息" class="btn btn-success" id="btn_dealer_search" />
							</div>

				          	
				          <div class="controls controls-row" style="text-align:center;">
				            	<label style="display: inline; margin-left: 10px;">经销商编号：</label><input id="dealer_id" style="width:200px;" type="text" class="m-wrap"  readonly>
				            	<label style="display: inline; margin-left: 10px;">经销商姓名：</label><input id="dealer_name" style="width:200px;" type="text" class="m-wrap">
				            	<label style="display: inline; margin-left: 10px;">默认返点：</label><input id="discount_rate" style="width:200px;" type="text" class="m-wrap" readonly>
				         		<label style="display: inline; margin-left: 10px;">法人代表：</label><input id="leal_person" style="width:200px;" type="text" class="m-wrap" readonly>
				          </div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="container-fluid" id="biz_dealer_comment"  style="display:none;">
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-title">
						<span class="icon"> <i class="icon-info-sign"></i>
						</span>
						<h5>经销商信息</h5>
					</div>
					<div class="widget-content nopadding" >
         				 <div class="widget-content nopadding">
           					 <table id="search_result" page_index="1" class="table table-bordered table-striped">
             					 <thead>
                					<tr>
                					  <th>选择</th>
                 					  <th>经销商姓名</th>
                  					  <th>经销商默认返点</th>
                  					  <th>业务对接人</th>
                  					  <th>大区</th>
                  					  <th>城市</th>
                  					  <th>余额</th>
                  					   <th>后返余额</th>
                                    </tr>
                                  </thead>
                                <tbody>
                               </tbody>
                            </table>
                    </div>
                     <button id='check_this_dealer' class="btn btn-success" type="submit">确认经销商</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<div class="row-fluid" id="address" select_address_id=""> 
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>收货地址选择　　　　<a href='#' id='add_address_href'>新增地址</a></h5>
          </div>
          <div id="address_lst_div" class="">
          </div>
      </div>

    </div>
  </div>
  
  <div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>商品选择</h5>
          </div>
          <div class="widget-content">
	          <ul class="recent-posts">
           <volist name="lst_product" id="product">
	          <li>
<!-- 	            <div class="user-thumb" style="margin-left:100px;">
					<img width="40" height="40" src="http://o.whaley.cn/Public/images/style/small/55.jpg" alt="User">
				</div> -->
				 <div class="article-post">
                  <div class="fr">
                  	<a href="#" class="icon-plus"></a>
                  	<input sku_id='{$product.sku_id}' is_rebate='{$product.is_rebate}' class="qty" type="text" style="width:40px;" value="0"/>
                  	<a href="#" class="icon-minus"></a>
				  </div>
				  <div style="" class="fr">
					  开 单 价：￥<span class="sale_price" style="text-decoration:line-through;">{$product.sale_price}</span><br>
					  溢&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;价：￥<input type="text" value="{$product.sale_price}" style="width: 60px; height: 12px; color: red; font-weight: bold;" class="biz_payment_price">
					  ￥<span class="discount_price" style="color:red;">{$product.sale_price}</span><br>
					</div>
                  <span style="color:red;" class="user-info">
      						<if condition="($product.is_rebate eq '0')">
      						  正价商品，无返点
      						<else/>
     						 </if>     
     						 
     					<if condition="($product.is_rebate eq '1')">
      						  参与返点
      						<else/>
     						 </if>         
                </span>
                  <div style="font-size:16px;font-weight:bold">{$product.sku_name} </div>
                </div>
	          </li>
	       </volist>   
              <li>
                <div class="article-post" style="height: 60px;font-size:16px;">
				  <div class="" style="float:right;">
					  商品数量：<span id='goods_qty' style="color:red;">0</span><br> 
					  商品金额：￥<span id='goods_amount' style="text-decoration:line-through;">0.00</span><br> 
					  支付金额：￥<span id='order_amount' style="color:red;">0.00</span><br>
					</div>
                </div>
              </li>
              <li style="text-align:center;">
                <button id='checkOutOrder' class="btn btn-success" type="submit">预览订单</button>
              </li>
            </ul>
          </div>
      </div>

    </div>
  </div>

</div>
</block>

<block name="js_content">
<script>
DealerInfoPage = {
		'BindEvent': function(){
			$('#btn_dealer_search').bind('click', function(){
				DealerInfoPage.SearchDealer();
			});
			
			$('#add_address_href').bind('click', function(){
				
				var dealer_user_id = $('#biz_dealer').attr('dealer_user_id');
				var dealer_channel =1;
				if(dealer_user_id==''){
				   alert('请加载经销商信息');
				   return;
				}
				window.location.href="/address/addressinfo?user_id="+dealer_user_id+"&dealer_channel="+dealer_channel;
			});
			
			$('#check_this_dealer').bind('click',function(){
				var deler_id = '';
				var deler_user_id = '';
				var deler_name = '';
				$('input[name="checkOneBox"]:checked').each(function(){ 
					deler_id=$(this).attr('deler_id'); 
					deler_user_id=$(this).attr('deler_user_id'); 
					deler_name=$(this).attr('deler_name'); 
				}); 
				
				if(deler_id ==''){
					alert('请选择经销商信息');
					return;
				}
				$("#biz_dealer_comment").css('display','none'); 
				DealerInfoPage.BuildPage(deler_id,deler_name,deler_user_id);
				

			});
			
			$('#checkOutOrder').bind('click',function(){
				var dealer_id = $('#biz_dealer').attr('dealer_id');
				if(dealer_id==''){
				   alert('请加载经销商信息');
				   return;
				}
				var select_address_id = $('#address').attr('select_address_id');
				
				if(select_address_id==''){
					   alert('地址信息未选择');
					   return;
				}
				
				var totalQty=$('#goods_qty').text();
				
				if(totalQty==0){
					 alert('商品未选择');
					 return;
				}
				
				var sku_list = Array();
				$('.recent-posts li').each(function(){
					if($(this).find('.qty').size()==0){
						return true;
					}

					var qty		= $(this).find('.qty').val();
					if(qty == 0){
						return true;
					}
					var sku_id	= $(this).find('.qty').attr('sku_id');

					var payment_price	= $(this).find('.biz_payment_price').val();
					sku_list.push(sku_id + "," + qty + "," + payment_price);
				});
				var sku_list_str = sku_list.join('|');
				
				var checkResult = DealerService.CheckDealerMinSalePrice(dealer_id,sku_list_str);
				if(checkResult.IsSuccess==0){
					alert(checkResult.ErrorMsg);
					return;
				}
				var docHeight = document.documentElement.clientHeight;

				
				window.location.href = '/Dealer/dealerOrderCheckout?dealer_id=' + dealer_id +
       		 '&select_address_id='+select_address_id+
    		 '&sku_list_str='+sku_list_str;
			});
			
			//绑定加减号
			$('.icon-plus').bind("click", function(){
				var curr_qty	= parseInt($(this).parents('li').find('.qty').val());
				var new_qty		= curr_qty + 1;
				$(this).parents('li').find('.qty').val(new_qty);
				DealerInfoPage.ReclacAmnount();
				return false;
			});
			
			//绑定加减号, .icon-minus
			$('.icon-minus').bind("click", function(){
				var curr_qty	= parseInt($(this).parents('li').find('.qty').val());
				var new_qty		= curr_qty - 1;
				if(new_qty < 0){
					new_qty = 0;
				}
				$(this).parents('li').find('.qty').val(new_qty);
				DealerInfoPage.ReclacAmnount();
				return false;
			});
			
			var before=1;
			
			$('.qty').bind('keydown',function(){
				before=$(this).val(); 
			});
			
			$('.qty').bind('keyup',function(){
				if(isNaN($(this).val())){
					alert('不是数字');
					$(this).val(before);
					return;
				}
				DealerInfoPage.ReclacAmnount();
			});
			
		},
		'SearchDealer':function(){
			var dealer_id = $('#dealer_id').val();
			var dealer_name=$('#dealer_name').val();
			var dealer_channel=1;
			var docHeight = document.documentElement.clientHeight;
			$("#biz_dealer_comment").css('display','block'); 
			
			
			var result = DealerService.SearchDearlerForOrder(dealer_id,dealer_name,dealer_channel);
			
			var dealer_lst=result.Result;
			
			var html = '';
			if(dealer_lst.length==0){
				html='暂无数据';
			}
			for(var index in dealer_lst){
				var dealer_en= dealer_lst[index];
				var tr_html = "<tr>";
				tr_html += "<td>" + "<input class='input_check_box' name='checkOneBox' deler_id="+dealer_en.dealer_id+" deler_name="+dealer_en.dealer_name + " deler_user_id=" + dealer_en.user_id+" type='radio' style='position:relative;width: 50px; opacity: 100;'>"+"</td>";
				tr_html += "<td>" + dealer_en.dealer_name + "</td>"
				tr_html += "<td>" + dealer_en.discount_rate + "</td>"
				tr_html += "<td>" + dealer_en.business_leader + "</td>"
				tr_html += "<td>" + dealer_en.area_name + "</td>"
				tr_html += "<td>" + dealer_en.city_name + "</td>"
				tr_html += "<td>" + dealer_en.amount + "</td>"
				tr_html += "<td>" + dealer_en.cashback_amount + "</td>"
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#search_result tbody').html('');
			$('#search_result tbody').append(html);
			

		},
		'ReclacAmnount':function(){
			var goods_qty		= 0;
			var goods_amount	= 0;
			var order_amount	= 0;
			$('.recent-posts li').each(function(){
				if($(this).find('.qty').size()==0){
					return true;
				}
				
				var sale_price		= parseFloat($(this).find('.sale_price').text());

				/**
				var discount_price 	= sale_price;
				var is_rebate	= $(this).find('.qty').attr('is_rebate');
				if(is_rebate == 0){
					discount_price = sale_price;
				}
				*/
				var discount_price	= parseFloat($(this).find('.biz_payment_price').val());
				var qty				= parseInt($(this).find('.qty').val());



				goods_qty	= goods_qty + qty;
				goods_amount= goods_amount + sale_price*qty;
				order_amount= order_amount + discount_price*qty;
			});
			
			$('#goods_qty').text(goods_qty);
			$('#goods_amount').text(goods_amount.toFixed(2));
			$('#order_amount').text(order_amount.toFixed(2));
		},
		'convertDealerType':function(obj){
			if(obj==1){
				return '一般纳税人';
			}else if(obj==2){
				return '个体工商户';
			}
				return '未知的类型';
		},'SelectAddress':function(obj){
			alert(obj);
		},'BuildPage':function(obj,dealer_name,dealer_user_id){
			var dealer_id =obj;
			var result = DealerService.AddDealerOrder(dealer_id);
			$('#dealer_id').val(dealer_id);
			$('#dealer_name').val(result.Result.dealer.dealer_name) ;
			$('#dealer_type').val(DealerInfoPage.convertDealerType(result.Result.dealer.dealer_type)) ;
			$('#discount_rate').val(result.Result.dealer.discount_rate);
			$('#business_leader').val(result.Result.dealer.business_leader);
			$('#business_leader_cellphone').val(result.Result.dealer.business_leader_cellphone);
			$('#area_name').val(result.Result.dealer.area_name);
			$('#province_name').val(result.Result.dealer.province_name);
			$('#leal_person').val(result.Result.dealer.leal_person) ;
			$('#biz_dealer').attr('dealer_id', dealer_id);
			$('#biz_dealer').attr('dealer_user_id', dealer_user_id);

			var lst_address = result.Result.address;
			var html = '';
			for(var index in lst_address){
				var user_address_en = lst_address[index];
				var div_html ="<div class='biz_address_div' address_id='" +user_address_en['address_id'] +"' style='padding: 5px; cursor: pointer; width: 80%; '>"; 
				div_html+="<div class='alert alert-success alert-block' style='margin-bottom:5px;height: 120px;width: 100%;'>"+"<a href='#' class='addressSelect'>未选择</a>";
				div_html+="<h4 class='alert-heading'>"+user_address_en['consignee_name']+"【"+user_address_en['consignee_cellphone']+"】</h4>";
				div_html+="<span style='font-size:16px;'>"+user_address_en['consignee_province']+user_address_en['consignee_city']+user_address_en['consignee_district']+user_address_en['consignee_address'] +"</span></div>";
				div_html+="</div>";
				html += div_html;
			}
			
			$('#address_lst_div').html('');
			$('#address_lst_div').append(html);
			
			//地址选定
			$(".biz_address_div").bind('click', function(){
				var address_id = $(this).attr('address_id');
				$('#address').attr('select_address_id', address_id);
				
				$(".biz_address_div").find('.alert').removeClass('alert-error');
				$(".biz_address_div").find('.alert').addClass('alert-success');
				$(".biz_address_div").find('.addressSelect').text('未选择');
				
				
				$(this).find('.alert').removeClass('alert-success');
				$(this).find('.alert').addClass('alert-error');
				
				$(this).find('.addressSelect').text('已选择');
				
				return false;
			});
			
			//改变价格
			var discount_rate = parseFloat($('#discount_rate').val());
			$('.recent-posts li').each(function(){
				var sale_price		= parseFloat($(this).find('.sale_price').text());
				var discount_price	= sale_price - sale_price*discount_rate;
				
				var is_rebate	= $(this).find('.qty').attr('is_rebate');

				if(is_rebate==0){
					discount_price = sale_price;
				}
				
				$(this).find('.discount_price').text(discount_price.toFixed(2));
			});
			
			DealerInfoPage.ReclacAmnount();
			
		}
};


$(document).ready(function(){
	var param_dealer_id = {$param_dealer_id};
	if(param_dealer_id!=''){
		var param_user_id = {$param_user_id};
		var param_deler_name = {$param_dealer_name};
		DealerInfoPage.BuildPage(param_dealer_id,param_deler_name,param_user_id);
	}
	DealerInfoPage.BindEvent();
	DealerInfoPage.ReclacAmnount();
});


</script>
<script src="/Public/js/layer/layer.js"></script>
</block>